var express = require('express');
var router = express.Router();

var movieModel = require('../models/movie');
var user = require('../models/user');
const { token } = require('morgan');
var article = require('../models/article');
var recommend = require('../models/recommend');

// 新增影片
router.post('/movies', async (req, res, next) => {
    if (!req.body.userName) {
        res.json({ status: 0, message: "用户不存在" });
        return;
    }
    if (!req.body.token) {
        res.json({ status: 0, message: "用户未登录" });
        return;
    }
    if (!req.body.userId) {
        res.json({ status: 0, message: "用户id不存在" });
        return;
    }
    if (!req.body.movieName) {
        res.json({ status: 0, message: "电影名称不存在" });
        return;
    }
    if (!req.body.movieImg) {
        res.json({ status: 0, message: "电影图片不存在" });
        return;
    }
    if (!req.body.movieDownload) {
        res.json({ status: 0, message: "电影下载地址不存在" });
        return;
    }
    const findUser = user.find({ _id: req.body.userId, userName: req.body.userName });
    if (!findUser) {
        res.json({ status: 0, message: '用户不存在' })
    }
    if (!checkUserPower(req.body.userId, req.body.userName, req.body.token)) {
        res.json({ status: 0, message: '用户权限不足' });
        return;
    }
    var saveMovie = new movieModel({
        movieName: req.body.movieName,
        movieImg: req.body.movieImg,
        movieVideo: req.body.movieVideo,
        movieDownload: req.body.movieDownload,
        movieTime: Date.now(),
        movieNumSuppose: 0,
        movieNumDownload: 0,
        movieMainPage: req.body.movieMainPage == true ? true : false
    });
    const newMovie = saveMovie.save();
    res.json({ status: 0, message: '新增电影成功', data: newMovie });
})

// 删除影片
router.delete('/movies/:movieId', async (req, res) => {
    if (!req.body.userId) {
        res.json({ status: 1, message: '用户id不存在' });
        return;
    }
    if (!req.body.userName) {
        res.json({ status: 1, message: '用户名称不存在' });
        return;
    }
    if (!req.body.userId) {
        res.json({ status: 1, message: '用户token不存在' });
        return;
    }
    if (!req.params.movieId) {
        res.json({ status: 1, message: '电影id不存在' });
        return;
    }
    var findUser = user.findByUserName(req.body.userName);
    if (!findUser || findUser.userStop == 0 || findUser.userAdmin == 0) {
        res.json({ status: 0, message: '用户无权限' });
        return;
    }
    await movieModel.deleteOne({ _id: req.params.movieId });
    res.json({ status: 0, message: '删除影片成功' });
});

// 修改影片
router.put('/movies/:movieId', async (req, res, next) => {
    try {
        if (!req.body.userId) {
            res.json({ status: 1, message: '用户id不存在' });
            return;
        }
        if (!req.body.userName) {
            res.json({ status: 1, message: '用户名称不存在' });
            return;
        }
        if (!req.body.token) {
            res.json({ status: 1, message: '用户token不存在' });
            return;
        }
        if (!req.params.movieId) {
            res.json({ status: 1, message: '更新的电影id不存在' });
            return;
        }
        var updateMovie = req.body.movieInfo;
        movieModel.updateOne({ _id: req.params.movieId }, req.body.movieInfo).exec();
        res.json({ status: 0, message: '更新成功' });
    } catch (error) {
        console.log(error);
        res.status(500).json({ status: 1, message: '服务器错误' });
    }
})

router.get('/movies', (req, res) => {
    movieModel.findAll()
        .then(movies => {
            res.json({ status: 0, message: "查询结果", data: movies });
        });
});



function checkUserPower(id, userName, token) {
    if (!id) {
        return false;
    }
    if (!userName) {
        return false;
    }
    if (!token) {
        return false;
    }
    return true;
}

router.post('/articles', async (req, res) => {
    if (!req.body.userId) {
        res.json({ status: 1, messge: '用户id不存在' });
        return;
    }
    if (!req.body.userName) {
        res.json({ status: 1, messge: '用户名称不存在' });
        return;
    }
    if (!req.body.token) {
        res.json({ status: 1, messge: 'token不存在' });
        return;
    }
    if (!req.body.articleTitle) {
        res.json({ status: 1, messge: '文章标题不存在' });
        return;
    }
    if (!req.body.articleContent) {
        res.json({ status: 1, messge: '文章内容不存在' });
        return;
    }
    if (!req.body.userId) {
        res.json({ status: 1, messge: '用户id不存在' });
        return;
    }
    var saveArticle = new article({
        articleTitle: req.body.articleTitle,
        articleContent: req.body.articleContent,
        articleTime: Date.now()
    });
    await saveArticle.save();
    res.json({ status: 0, message: '新增成功' });
})

router.delete('/articles/:id', async (req, res) => {
    if (!req.body.userId) {
        res.json({ status: 1, messge: '用户id不存在' });
        return;
    }
    if (!req.body.userName) {
        res.json({ status: 1, messge: '用户名称不存在' });
        return;
    }
    if (!req.body.token) {
        res.json({ status: 1, messge: 'token不存在' });
        return;
    }
    if (!req.params.id) {
        res.json({ status: 1, message: '文章id不存在' });
    }
    article.deleteOne({ _id: req.params.id }).exec();
    res.json({ status: 0, message: '删除成功' });
});

router.put('/articles/:id', async (req, res) => {
    try {
        if (!req.body.userId) {
            res.json({ status: 1, messge: '用户id不存在' });
            return;
        }
        if (!req.body.userName) {
            res.json({ status: 1, messge: '用户名称不存在' });
            return;
        }
        if (!req.body.token) {
            res.json({ status: 1, messge: 'token不存在' });
            return;
        }
        if (!req.params.id) {
            res.json({ status: 1, message: '文章id不存在' });
        }

        await article.updateOne({ _id: req.params.id }, req.body.articleInfo);
        res.json({ status: 0, message: '更新成功' });

    } catch (err) {
        console.log(err);
        res.json({ status: 1, message: '文章更新报错 + err  ' });
    }
})

router.get('/articles', async (req, res) => {
    var articles = await article.find({});
    res.json({ status: 0, data: articles, message: '查询成功' });
});

router.post('/recommends', async (req, res) => {
    try {
        if (!req.body.userId) {
            res.json({ status: 1, messge: '用户id不存在' });
            return;
        }
        if (!req.body.userName) {
            res.json({ status: 1, messge: '用户名称不存在' });
            return;
        }
        if (!req.body.token) {
            res.json({ status: 1, messge: 'token不存在' });
            return;
        }

        if (!req.body.recommendImg) {
            res.json({ status: 1, messge: '推荐图片内容不存在' });
            return;
        }
        if (!req.body.recommendSrc) {
            res.json({ status: 1, messge: '推荐图片地址不存在' });
            return;
        }
        if (!req.body.recommendTitle) {
            res.json({ status: 1, messge: '推荐图片标题不存在' });
            return;
        }
        var saveRecommend = new recommend({
            recommendImg: req.body.recommendImg,
            recommendSrc: req.body.recommendSrc,
            recommendTitle: req.body.recommendTitle
        });
        await saveRecommend.save();
        res.json({ status: 0, message: '新增成功' });
    } catch (err) {
        console.error(err);
        res.json({ status: 1, message: '新增失败' });
    }
})

router.delete('/recommends/:id', async (req, res) => {
    try {
        if (!req.body.userId) {
            res.json({ status: 1, messge: '用户id不存在' });
            return;
        }
        if (!req.body.userName) {
            res.json({ status: 1, messge: '用户名称不存在' });
            return;
        }
        if (!req.body.token) {
            res.json({ status: 1, messge: 'token不存在' });
            return;
        }

        if (!req.params.id) {
            res.json({ status: 1, messge: '推荐id不存在' });
            return;
        }
        await recommend.deleteOne({ _id: req.params.id });
        res.json({ status: 0, message: '删除成功' });
    } catch (err) {
        console.error(err);
        res.json({ status: 1, message: '删除失败' });
    }
});

router.put('/recommends/:id', (req, res) => {
    if (!req.body.userId) {
        res.json({ status: 1, messge: '用户id不存在' });
        return;
    }
    if (!req.body.userName) {
        res.json({ status: 1, messge: '用户名称不存在' });
        return;
    }
    if (!req.body.token) {
        res.json({ status: 1, messge: 'token不存在' });
        return;
    }

    if (!req.params.id) {
        res.json({ status: 1, messge: '推荐id不存在' });
        return;
    }

    recommend.updateOne({ _id: req.params.id }, req.body.recommendInfo)
        .then(result => {
            res.json({ status: 0, message: '更新成功' });
        })
        .catch(err => {
            console.error(err);
            res.json({ status: 1, message: '更新失败' });
        });
})

router.get('/recommends', (req,res) => {
    recommend.findAll()
    .then(recommends => {
        res.json({status: 0, message: '查询成功', data: recommends});
    })
    .catch( err => {
        res.json({status: 1, message: '查询失败'});
    })
});

module.exports = router;